package com.supinfo.youfood.forms;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;

import com.supinfo.food.entities.Magasin;
import com.supinfo.food.services.MagasinService;
import com.supinfo.food.services.MagasinServiceService;
import com.supinfo.food.services.MagasinServiceServiceLocator;

/**
 * Formulaire d'ajout de magasin
 * @author Keitaro
 */
@SuppressWarnings("rawtypes")
public class AddMagasinForm extends Form {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass());
	private List<String> countries;
	
	@SuppressWarnings("unchecked")
	public AddMagasinForm(String id, IModel model) {
		super(id, model);
		
		TextField name = new TextField("name");
		name.setRequired(true);
		add(name);
		
		autoCompletion();
	}
	
	/**
	 * Autocompletion
	 * @return 
	 */
	private void autoCompletion() {
		// Webservice
		try {
			MagasinServiceService binding = new MagasinServiceServiceLocator();
			MagasinService service = binding.getMagasinService();
			countries = Arrays.asList(service.getCountries());
		} catch (ServiceException e) {
			logger.info("Service can not be reached.");
		} catch (RemoteException e) {
			logger.info("Service does not responded.");
		}
		
		// Autocompletion
		final AutoCompleteTextField<String> location = new AutoCompleteTextField<String>("location")
	        {
				private static final long serialVersionUID = 1L;
				@Override
	            protected Iterator<String> getChoices(String input)
	            {
	                if (Strings.isEmpty(input))
	                {
	                    List<String> emptyList = Collections.emptyList();
	                    return emptyList.iterator();
	                }
	                List<String> choices = new ArrayList<String>(10);
	                for (String country : countries)
	                {
	                    if (country != null && !country.isEmpty() && country.toUpperCase().startsWith(input.toUpperCase()))
	                    {
	                        choices.add(country);
	                        if (choices.size() == 10)
	                            break;
	                    }
	                }

	                return choices.iterator();
	            }
	        };
	        location.setRequired(true);
	        add(location);
	}
	
	@Override
	protected void onSubmit() {
		Magasin user = (Magasin) getModelObject();
		try {
			MagasinServiceService binding = new MagasinServiceServiceLocator();
			MagasinService service = binding.getMagasinService();
			service.add(user);
			info("Magasin has been created.");
		} catch (ServiceException e) {
			logger.info("Service can not be reached.");
		} catch (RemoteException e) {
			logger.info("Service does not responded.");
		}
		
	}
}
